	package fragments;

	import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.ArrayList;
	import java.util.List;

import network.NetworkHandler;
	
	import stockrobot.android.R;
import android.settingsActivity;
import android.R.id;
import android.content.Intent;
	import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
	import android.support.v4.app.Fragment;
	import android.view.LayoutInflater;
	import android.view.Menu;
	import android.view.MenuInflater;
	import android.view.MenuItem;
	import android.view.View;
	import android.view.ViewGroup;
	import android.widget.ArrayAdapter;
	import android.widget.LinearLayout;
	import android.widget.ListView;
import android.widget.Toast;

	public class AstroLogFragment extends Fragment implements PropertyChangeListener {
		ArrayAdapter<String> adapter;
	    /** Called when the activity is first created. */
	    public View onCreateView(LayoutInflater inflater, ViewGroup container,
				Bundle savedInstanceState) {
		    if(container == null){
			    return null;
		    }
			return (LinearLayout)inflater.inflate(R.layout.astrolog, container, false);
		}
	    
	    @Override
	    public void onActivityCreated(Bundle bundle) {
		    final ListView log = (ListView) getActivity().findViewById(R.id.astroLogListView);
		    adapter = new ArrayAdapter<String>(getActivity(),R.layout.log_listrow, android.R.id.text1, new ArrayList<String>());
		    if(NetworkHandler.instance().isFakeServer()){
		    	populateLog();
		    	populateLog();
		    } else {
				NetworkHandler.instance().addObserver(this);
				ArrayList<String> logData = NetworkHandler.instance().getAllLogEvents();
				
				for(String s : logData){
					adapter.insert(s, 0);
				}
		    }
	    	log.setAdapter(adapter);
	    	setHasOptionsMenu(true);
	    	super.onActivityCreated(bundle);
	    }
	    
	    public void populateLog(){
	    	adapter.add("Starting Astro");
		    adapter.add("Bought 10 VOlvo B stocks for 2452.542");
		    adapter.add("Got new stock data from Harvester.");
		    adapter.add("Running algorithms.");
		    adapter.add("Updating stocks...");
		    adapter.add("Finished updating.");
		    adapter.add("Bought 42 Ericsson B for 2523.");
		    adapter.add("Starting Astro");
		    adapter.add("Starting Astro");
		    adapter.add("Starting Astro");
		    adapter.add("Starting Astro");
		    adapter.add("Bought 10 VOlvo B stocks for 2452.542");
		    adapter.add("Got new stock data from Harvester.");
		    adapter.add("Running algorithms.");
		    adapter.add("Updating stocks...");
		    adapter.add("Finished updating.");
		    adapter.add("Bought 42 Ericsson B for 2523.");
		    adapter.add("Starting Astro");
		    adapter.add("Starting Astro");
		    adapter.add("Starting Astro");
		    adapter.insert("First?!", 0);
	    }
	    
	 	public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
	 		inflater.inflate(R.menu.astrolog_menu, menu);
	 		super.onCreateOptionsMenu(menu, inflater);
	 	}
	 	
	    @Override
	    public boolean onOptionsItemSelected(MenuItem item) {
	    	// Handle item selection    
	    	switch (item.getItemId()) {
	    		case R.id.menu_item_logdisconnect: 
	    			disconnect();
	    			return true;
	    		case R.id.menu_item_logabout: 
	    			populateLog();
	    			return true;
	    		case R.id.menu_item_clearlog: 
	    			clearLog();
	    			return true;
	    		case R.id.menu_item_settings: 
	    			showSettings();
	    			return true;
	    		default:
	    			return super.onOptionsItemSelected(item);    
	    		}
	    	}

	    private void showSettings() { 
		    Intent prefIntent = new Intent(getActivity(),settingsActivity.class);   
		    this.startActivity(prefIntent); 
		}
	    private void clearLog() {
	    	adapter.clear();
		}

		private void showAbout() {
			Toast.makeText(getActivity(), "ASTRo Stock-Robot android by Erik Ramqvist", Toast.LENGTH_SHORT).show();
		}
	    
	 	private void disconnect() {
	 		NetworkHandler.instance().disconnect();
			  Toast.makeText(getActivity(), "Disconnected.", Toast.LENGTH_SHORT).show();
			  getActivity().finish();

		}

		public void propertyChange(final PropertyChangeEvent event) {
			Handler refresh = new Handler(Looper.getMainLooper()); 
			refresh.post(new Runnable() { 
			    public void run() 
			    { 
			        handleEvent(event); 
			    }

				private void handleEvent(PropertyChangeEvent event) {
					if(event.getPropertyName().equals("LogItem")){
						adapter.insert((String) event.getNewValue(), 0);
					}
				} 
			}); 


		} 


	}
